import React, {Component,} from "react";
import Admin from "../../../layout/Admin";
import DownloadModal from "../../../model/download";
import {Input, PageHeader} from "antd";
import {Button, Form,} from "antd";
import Info from "../../../service/info";

class Download extends Component {
    constructor(props) {
        super(props)
        this.model = new DownloadModal()
        this.state = {
            form: {
                'web': '',
                'ios': '',
                'aod': '',
            },
        }
    }

    componentDidMount() {
        this.get()
    }

    get = () => {
        this.model
            .get()
            .then((resp) => {
                const data = resp.data.ok
                const {
                    web,
                    ios,
                    aod,
                } = data
                this.setState({
                    form: {
                        web,
                        ios,
                        aod,
                    },
                })
            })
    }

    set = (params) => {
        this.model
            .set(params)
            .then((resp) => {
                console.log(resp)
                if (resp.data.ok === 1) {
                    Info.success('修改成功')
                }
            })
            .then(() => {
                this.get()
            })
    }

    onSet = (values) => {
        this.set(values)
    }



    render() {
        const header = <div style={{background: '#fff'}}>
            <PageHeader title='下载管理'/>
        </div>

        const formValues = this.state.form

        const layout = {
            labelCol: { span: 4 },
            wrapperCol: { span: 16 },
        }
        const tailLayout = {
            wrapperCol: { offset: 4, span: 16 },
        }
        console.log(this.state)
        const form = <Form
            {...layout}
            ref={el => this.form = el}
            initialValues={formValues}
            onFinish={this.onSet}
        >
            <Form.Item
                label="h5 下载页"
                name="web"
            >
                <Input placeholder={this.state.form.web}/>
            </Form.Item>
            <Form.Item
                label="ios 下载地址"
                name="ios"
            >
                <Input placeholder={this.state.form.ios}/>
            </Form.Item>
            <Form.Item
                label="安卓 下载地址"
                name="aod"
            >
                <Input placeholder={this.state.form.aod}/>
            </Form.Item>
            <Form.Item
                {...tailLayout}
            >
                <Button type="primary" htmlType="submit">
                    设置
                </Button>
            </Form.Item>
        </Form>

        return <Admin>
            {header}
            <div className='list-container'>
                {form}
            </div>
        </Admin>
    }
}

export default Download
